var ECharts;
$(document).ready(function () {
    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma',
        }
    });
    require(
        [
            'echarts',
            'roma'
        ]);
    //加载方法
    process();
});
var processList = [];//接口
processList.push("FM-10-32-001-01");
processList.push("PM-10-32-001-01");
processList.push("PM-10-32-001-02");

//进程
function process() {
    var str = processList.toString();
    $.ajax({
        url: ctx + "/load/queryTable",
        type: "post",
        data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&RESU=" + str + "&DATE_TYPE=" + DATE_TYPE,
        success: function (data) {
            var map = JSON.parse(data);
            var list = map["FM-10-32-001-01"];
            var list1 = map["PM-10-32-001-01"];
            var list2 = map["PM-10-32-001-02"];
            var table = "";
            if (list.length == 0 && list1.length == 0 && list2.length == 0) {
                table +=
                    "<tr><th><span >" + "指标名" + "</span></th></tr>" +
                    "<tr><td><span >" + "暂无数据" + "</span></td></tr>";
            } else {
                table += "<tr>" +
                    "<th><span >" + "Program" + "</span></th>" +
                    "<th><span >" + list[0].KPI_NAME + "</span></th>" +
                    "<th><span >" + list1[0].KPI_NAME + "</span></th>" +
                    "<th><span >" + list2[0].KPI_NAME + "</span></th>" +
                    "</tr>";
                if (DATE_TYPE == "showYear") {
                    var UnitIdList = new Array()   //结果ID列表
                    var AvgValueList1 = new Array() //结果VALUE列表
                    var AvgValueList2 = new Array();
                    var succList = new Array();
                    var countList = new Array();
                    var map = new Map()            //结果map
                    var iSameLine = 0 //相同条数标志位

                    var SumValue1 = 0 //和值
                    var SumValue2 = 0;
                    var gsuccValue = 0
                    var gcountValue = 0
                    var sTreeUnitId   //上一次ID

                    var str           //原始unit_id
                    var sUnitId       //Unit_id
                    var succValue
                    var countValue
                    var AvgValue1     //avg_value
                    var AvgValue2   //list1的value
                    var sc
                    for (i = 0; i < list.length; i++) {
                        str = list[i].UNIT_ID;
                        sUnitId = str.split("-")[4];
                        if (i < list.length && i < list1.length && i < list2.length) {
                            if (list[i].SUCCESS_VALUE == undefined) {
                                succValue = 0;
                            }
                            countValue = parseInt(list[i].COUNT_VALUE);
                            if (list1[i].AVG_VALUE == undefined) {
                                AvgValue1 = 0;
                            }
                            if (list2[i].AVG_VALUE == undefined) {
                                AvgValue2 = 0;
                            }//获取对应的avg_value/
                        }
                        if (i == 0) {                    //第一次进入标志位强制相同
                            sTreeUnitId = sUnitId
                        }
                        if (sTreeUnitId == sUnitId) {   //本次循环获取id与上次相同累加并继续下次循环
                            SumValue1 = SumValue1 + AvgValue1;
                            SumValue2 = SumValue2 + AvgValue2;
                            gsuccValue = gsuccValue + succValue;
                            gcountValue = gcountValue + countValue;
                            iSameLine = iSameLine + 1     //桩相同条数
                            if (i == list.length - 1) {   //循环到最后一次如果与上一次相同直接写出结果
                                UnitIdList.push(sTreeUnitId)
                                succList.push(gsuccValue);
                                countList.push(gcountValue);
                                AvgValueList1.push(SumValue1)
                                AvgValueList2.push(SumValue2);
                                map.set(sTreeUnitId, SumValue1)
                            }
                        } else {
                            UnitIdList.push(sTreeUnitId)
                            succList.push(gsuccValue);
                            countList.push(gcountValue);
                            AvgValueList1.push(SumValue1)
                            AvgValueList2.push(SumValue2);
                            map.set(sTreeUnitId, SumValue1)
                            iSameLine = 1
                            sTreeUnitId = sUnitId
                            SumValue1 = AvgValue1
                            SumValue2 = AvgValue2;
                            gsuccValue = succValue;
                            gcountValue = countValue;
                        }

                    }
                    for (var c = 0; c < UnitIdList.length; c++) {
                        if (c < UnitIdList.length) {
                            table += "<tr>" +
                                "<td><span >" + UnitIdList[c] + "</span></td>" +
                                "<td><span >" + succList[c] / countList[c] * 100 + "%" + "</span></td>" +
                                "<td><span >" + AvgValueList1[c] + "秒" + "</span></td>" +
                                "<td><span >" + AvgValueList2[c] + "秒" + "</span></td>" +
                                "</tr>";
                        }
                    }
                    $("#row7").css("height", (UnitIdList.length + 1) * 34 + 14 + 34 + 34);
                } else {
                    for (var i = 0; i < list.length; i++) {
                        var str = list[i].UNIT_ID;
                        var UNITID = str.split("-")[4];
                        if (list[i].SUCCESS_VALUE == undefined) {
                            sc = 0;
                        }
                        if (list[i].AVG_VALUE == undefined) {
                            AvgValue1 = 0;
                        }
                        if (list[i].AVG_VALUE == undefined) {
                            AvgValue2 = 0;
                        }
                        table += "<tr>" +
                            "<td><span >" + UNITID + "</span></td>" +
                            "<td><span >" + parseInt((sc / list[i].COUNT_VALUE) * 100) + "%" + "</span></td>" +
                            "<td><span >" + AvgValue1 + "秒" + "</span></td>" +
                            "<td><span >" + AvgValue2 + "秒" + "</span></td>" +
                            "</tr>";
                    }
                    $("#row7").css("height", (list.length + 1) * 34 + 14 + 34 + 34);
                }
            }
            $("#ogg").append(table);
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

// $(".rad_1").click(function () {
//     var flag = $(this).attr('for');
//     $("#ogg tr").remove();
//
//     process(flag);
//
// });